package com.lvchen.ui.login;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.WindowManager;

import com.lvchen.R;
import com.lvchen.common.MyApplication;
import com.lvchen.common.base.AppManager;
import com.lvchen.common.base.BaseActivity;
import com.lvchen.common.myinterface.DiologState;
import com.lvchen.common.utils.UIHelper;
import com.lvchen.ui.main.MainActivity;
import com.othershe.dutil.DUtil;
import com.othershe.dutil.callback.DownloadCallback;


import java.io.File;


/**
 * 启动页
 */
public class SplashActivity extends BaseActivity {

    private String appdownloadurl = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        goToMainPage();

    }

    private void getData(){

        UIHelper.showcommondialog(SplashActivity.this, "温馨提示", "有新版本辣", "确定", "取消", new DiologState() {
            @Override
            public void success() {

                DUtil.init(SplashActivity.this)
                        .url(appdownloadurl)
                        .path(Environment.getExternalStorageDirectory()+"")
                        .name(UPDATE_SERVERAPK)
                        .childTaskCount(1)
                        .build()
                        .start(new DownloadCallback() {
                            @Override
                            public void onStart(long l, long l1, float v) {

                            }

                            @Override
                            public void onProgress(long l, long l1, float v) {

                            }

                            @Override
                            public void onPause() {

                            }

                            @Override
                            public void onCancel() {

                            }

                            @Override
                            public void onFinish(File file) {

                            }

                            @Override
                            public void onWait() {

                            }

                            @Override
                            public void onError(String s) {

                            }
                        });
            }

            @Override
            public void failure() {

            }
        });



    }


    private void goToMainPage() {
        //TODO 前往主页
        MainActivity.goToThisActivity(SplashActivity.this);
        AppManager.getAppManager().finishActivity();
    }

    private void goToLoginPage() {
        //TODO 前往登陆页
//         LoginActivity.goToThisActivity(SplashActivity.this, 0);
        AppManager.getAppManager().finishActivity();

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    update();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    /**
     * 下载完成，通过handler将下载对话框取消
     */
    public void down() {
        new Thread() {
            public void run() {
                handler.sendEmptyMessage(0);
            }
        }.start();
    }

    /**
     * 下载apk
     */
    private String UPDATE_SERVERAPK = "lvchen.apk";

    /**
     * 安装应用
     */
    public void update() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), UPDATE_SERVERAPK)), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

}
